﻿using GSOrganization.Application;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http.Headers;

namespace GSOrganization.Web.Entry.Controllers
{
    [ApiController]
    [Route("FileApi")]
    [AllowAnonymous]
    public class FileApiController : Controller
    {
        private static IConfiguration _config = null;
        private readonly IWebHostEnvironment _environment;

        public FileApiController(IConfiguration configuration, IWebHostEnvironment environment)
        {
            _config = configuration;
            _environment = environment;
        }

        [HttpPost("Enclosure")]
        public JsonResult AddImg()
        {
            var value = UploadImage("Upload");

            return Json(new { code = string.IsNullOrEmpty(value) ? 500 : 200, msg = "", value = new { value = string.IsNullOrEmpty(value) ? "" : value, host = _config["HostUrl"] } });
        }

        private string UploadImage(string directoryName)
        {
            var name = "";

            if (Request.Form.Files.Count > 0)
            {
                var files = Request.Form.Files;
                foreach (var file in files)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    string filePath = _environment.WebRootPath + $@"\{directoryName}\";
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    //文件后缀
                    var fileExtension = Path.GetExtension(fileName);
                    fileName = Guid.NewGuid() + fileExtension;

                    string fileFullName = filePath + fileName;

                    using (var stream = new FileStream(fileFullName, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                    name = $@"/{directoryName}/" + fileName;
                    break;
                }
            }

            return name;
        }

        [HttpPost("UploadFile")]
        public JsonResult AddFile()
        {
            var fileinfo = UploadImageNew("Upload");

            return Json(new { code = fileinfo == null ? 500 : 200, msg = "", value = fileinfo });
        }

        private FileInfo UploadImageNew(string directoryName)
        {
            FileInfo fileinfo = new FileInfo();
            
                if (Request.Form.Files.Count > 0)
                {
                    var files = Request.Form.Files;
                    foreach (var file in files)
                    {
                        var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        fileinfo.FName = fileName;
                        fileinfo.FId = Result.GetNewId();
                        string filePath = _environment.WebRootPath + $@"\{directoryName}\";
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }

                        //文件后缀
                        var fileExtension = Path.GetExtension(fileName);
                        fileName = Guid.NewGuid() + fileExtension;
                        string fileFullName = filePath + fileName;

                        using (var stream = new FileStream(fileFullName, FileMode.Create))
                        {
                            file.CopyTo(stream);
                        }
                        fileinfo.FPath = $@"/{directoryName}/" + fileName;
                        break;
                    }
                }
            
            return fileinfo;
        }
    }

    public class FileInfo
    {
        public string FId { get; set; }
        public string FName { get; set; }
        public string FPath { get; set; }
    }
}
